package com.tools.ad.strategy;

import com.hm.base.android.mob.AMApplication;
import com.tools.ad.ADModule;
import com.tools.ad.EntranceType;
import com.tools.ad.strategy.inner.InnerCommonAdExecutor;
import com.tools.ad.strategy.outer.AppOuterDeferExecutor;
import com.tools.ad.strategy.outer.AppOuterExecutor;
import com.tools.ad.strategy.outer.OuterCommonAdExecutor;
import com.tools.ad.strategy.outer.SwipeCloseExecutor;
import com.tools.ad.strategy.outer.SwipeExecutor;
import com.tools.ad.strategy.outer.WeatherCloseExecutor;
import com.tools.ad.strategy.outer.WeatherExecutor;

import java.util.HashMap;

/**
 * desc: 广告位 管理器
 * date: 2017/12/19 15:34
 * author: ancun
 */
public class ADStrategyManager {

    private static final String TAG = "ADStrategyManager";

    private AMApplication imContext;
    private ADModule mAdModule;

    private HashMap<String, Object> mExecutorMap;

    public ADStrategyManager(AMApplication context, ADModule adModule) {
        imContext = context;
        mAdModule = adModule;
        mExecutorMap = new HashMap<>();
    }

    private <T> T getBean(Class<T> clazz, String mark) {
        String key = clazz.getName() + mark;
        Object instance = mExecutorMap.get(key);
        if (instance == null) {
            synchronized (clazz) {
                instance = mExecutorMap.get(key);
                if (instance == null) {
                    instance = this.createBean(clazz, mark);
                    mExecutorMap.put(key, instance);
                }
            }
        }
        return (T) instance;
    }

    private <T> T createBean(Class<T> clazz, String mark) {
        Object obj = null;

        if (clazz == InnerCommonAdExecutor.class) {
            obj = new InnerCommonAdExecutor(imContext, mAdModule, mark);

        } else if (clazz == OuterCommonAdExecutor.class) {
            obj = new OuterCommonAdExecutor(imContext, mAdModule, mark);

        } else if (clazz == WeatherExecutor.class) {
            obj = new WeatherExecutor(imContext, mAdModule, mark);

        } else if (clazz == SwipeExecutor.class) {
            obj = new SwipeExecutor(imContext, mAdModule, mark);

        } else if (clazz == AppOuterExecutor.class) {
            obj = new AppOuterExecutor(imContext, mAdModule, mark);

        } else if (clazz == FunctionStrategyExecutor.class) {
            obj = new FunctionStrategyExecutor(imContext, mAdModule, mark);

        } else if (clazz == AppOuterDeferExecutor.class) {
            obj = new AppOuterDeferExecutor(imContext, mAdModule, mark);
        } else if (clazz == SwipeCloseExecutor.class) {
            obj = new SwipeCloseExecutor(imContext, mAdModule, mark);
        } else if (clazz == WeatherCloseExecutor.class) {
            obj = new WeatherCloseExecutor(imContext, mAdModule, mark);
        }

        return (T) obj;
    }


    public IStrategyExecutor getExecutorByEntranceType(EntranceType entranceType) {
        switch (entranceType) {
            case SPLASH:
            case TRIGGER:
            case LANDING:
            case HOME_TRASH_COMPLETED:
            case HOME_TRASH_INTERSTITIAL:
            case HOME_POWER_COMPLETED:
            case HOME_POWER_INTERSTITIAL:
            case HOME_CPU_COMPLETED:
            case HOME_CPU_INTERSTITIAL:
            case HOME_BOOST_COMPLETED:
            case HOME_BOOST_INTERSTITIAL:
            case LANDING_INTERSTITIAL:
                return getBean(InnerCommonAdExecutor.class, entranceType.getName());

            case WEATHER_FULL_SCREEN:
            case APP_INSTALL:
            case APP_UNINSTALL:
            case AUTO_CLEAN:
                return getBean(OuterCommonAdExecutor.class, entranceType.getName());

            case WEATHER:
                return getBean(WeatherExecutor.class, entranceType.getName());

            case SWIPE:
                return getBean(SwipeExecutor.class, entranceType.getName());

            case APP_OUTER_AD:
                return getBean(AppOuterExecutor.class, entranceType.getName());

            case FUNCTION_BOOST:
            case FUNCTION_POWER:
            case FUNCTION_COOL:
                return getBean(FunctionStrategyExecutor.class, entranceType.getName());

            case APP_INSTALL_DEFER:
            case APP_UNINSTALL_DEFER:
            case AUTO_CLEAN_DEFER:
                return getBean(AppOuterDeferExecutor.class, entranceType.getName());
            case SWIPE_CLOSE:
                return getBean(SwipeCloseExecutor.class, entranceType.getName());
            case WEATHER_CLOSE:
                return getBean(WeatherCloseExecutor.class, entranceType.getName());
            default:
                return null;
        }
    }

}